#include<bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
const int N=2e5+10;
#define INF 0x3f3f3f3f;
typedef long long int ll;
#define close(); std::ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);
//----------------------------------------------------------------------------//
int n,m;
void solve()
{
	cin>>n>>m;
	string a,b;
	cin>>a>>b;
	a+='?';
	b+='?';
	
	bool pos1=0;
	bool pos2=0;
	int cnt1=0;
	int cnt2=0;
	for(int i=0;i<n-1;i++) if(a[i]==a[i+1]) cnt1++,pos1=i;
	for(int i=0;i<m-1;i++) if(b[i]==b[i+1]) cnt2++,pos2=i;
	if(cnt1+cnt2>1) 
	{
		cout<<"NO\n";
		return;
	}
	if(cnt1+cnt2==0)
	{
		cout<<"YES\n";
		return;
	}
	
	if(a[n-1]==b[m-1])
	{
		cout<<"NO\n";
		return;
	}
	else
	{
		cout<<"YES\n";
		return;
	}

}

int main()
{
	close();
	int T; cin>>T;
	while (T--) solve();
	return 0;
}

